package com.gnerv.battle.boot.module.base.service.impl;

import com.gnerv.battle.boot.common.tools.DateTimeTools;
import com.gnerv.battle.boot.module.base.repository.mapper.NoticeMapper;
import com.gnerv.battle.boot.module.base.model.bo.NoticeBO;
import com.gnerv.battle.boot.module.base.model.dto.NoticeDTO;
import com.gnerv.battle.boot.module.base.model.entity.Notice;
import com.gnerv.battle.boot.module.base.service.CurrentAccountService;
import com.gnerv.battle.boot.module.base.service.NoticeService;
import lombok.AllArgsConstructor;
import lombok.NonNull;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.gnerv.battle.boot.common.tools.PageTools.limitSize;
import static com.gnerv.battle.boot.common.tools.PageTools.limitStart;

/**
 * @author Gnerv LiGen
 */
@Service
@AllArgsConstructor
public class NoticeServiceImpl implements NoticeService {

    private final NoticeMapper noticeMapper;
    private final CurrentAccountService currentAccountService;

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean create(@NonNull NoticeBO noticeBO) {
        Notice notice = noticeBO.toCreateNotice();
        notice.setCreator(currentAccountService.accountUkId());
        noticeMapper.insert(notice);
        return true;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean deleteByUkId(@NonNull String ukId) {
        noticeMapper.deleteByUkId(ukId);
        return true;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean updateByUkId(@NonNull NoticeBO noticeBO) {
        Notice notice = noticeBO.toUpdateNotice();
        noticeMapper.updateByUkId(notice);
        return true;
    }

    @Override
    @Transactional(readOnly = true)
    public NoticeDTO selectByUkId(@NonNull String ukId) {
        return noticeMapper.selectByUkId(ukId);
    }

    @Override
    @Transactional(readOnly = true)
    public List<NoticeDTO> listByTitle(String title, Integer page, Integer size) {
        return noticeMapper.listByTitle(title, limitStart(page, size), limitSize(size));
    }

    @Override
    @Transactional(readOnly = true)
    public List<NoticeDTO> listByAccountUkId(String accountUkId, Integer page, Integer size) {
        return noticeMapper.listByAccountUkId(accountUkId, limitStart(page, size), limitSize(size));
    }

    @Override
    @Transactional(readOnly = true)
    public List<NoticeDTO> listReceive(String accountUkId, String status, Integer page, Integer size) {
        return noticeMapper.listReceive(accountUkId, status, limitStart(page, size), limitSize(size));
    }

    @Override
    public int countByTitle(String title) {
        return noticeMapper.countByTitle(title);
    }

    @Override
    public int countByAccountUkId(String accountUkId) {
        return noticeMapper.countByAccountUkId(accountUkId);
    }

    @Override
    public int countReceive(String accountUkId, String status) {
        return noticeMapper.countReceive(accountUkId, status);
    }

    @Override
    public boolean send(String noticeUkId, List<String> accountUkIdList) {
        noticeMapper.send(noticeUkId, accountUkIdList, "unread", DateTimeTools.currentTimeMillis(), DateTimeTools.currentTimeMillis());
        return true;
    }

    @Override
    public NoticeDTO selectReceiveByAccountUkIdAndNoticeUkId(String accountUkId, String noticeUkId) {
        return noticeMapper.selectReceiveByAccountUkIdAndNoticeUkId(accountUkId, noticeUkId);
    }


}




